﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
            Dim sql As String = "SELECT * FROM profile WHERE TravellerName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_firstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_lastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_description.Text = dr("Description")
                If Not IsDBNull(dr("Ethnicity")) Then tb_ethnicity.Text = dr("Ethnicity")
            End If
        End If
    End Sub




    Protected Sub btn_saveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_saveProfile.Click
        Dim newFileName As String = ""
        Dim Sql As String


        If fu_picture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = (Request.MapPath("pictures") & "/" & newFileName)
            fu_picture.SaveAs(fileLocationOnServerHardDisk)
        End If


        Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        If fu_picture.HasFile Then
            Sql = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,Ethnicity=@f4,Picture=@f5 WHERE TravellerName=@f6"
        Else
            Sql = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,Ethnicity=@f4 WHERE TravellerName=@f6"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(Sql, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_description.Text)
        cmd.Parameters.AddWithValue("@f4", tb_ethnicity.Text)
        If fu_picture.HasFile Then cmd.Parameters.AddWithValue("@f5", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        conn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)
    End Sub
End Class